<?php
namespace app\app\controller\v1;
use Think\Controller;
use think\Db;
use think\Request;
use app\app\util\OnlineUtils;
class MyGiveController extends Controller{


    /**
     * @return array
     * [生成赠送记录]
     */
    public function record()
    {
        $give_record['user_id'] = OnlineUtils::getUserIdBySid();    //赠送人id
        $give_record['prop_id'] = input('get.prop_id');             //道具id
        $give_record['created_at'] = time();                        //赠送时间
        //判断在赠送人是否有该道具并且数量大于1
        $user_id_prop_num = Db::name('my_prop')->where('user_id','eq',$give_record['user_id'])->where('prop_id','eq',$give_record['prop_id'])->find();
        //如果道具数量大于1
        if( $user_id_prop_num['num'] >1 ){
            //赠送记录生成,在我的道具表中我的道具数量减一
            $record = DB::name('my_give')->insertGetId($give_record);
            Db::query('update ft_my_prop set num = num - 1 where user_id='.$give_record['user_id'].' and prop_id='.$give_record['prop_id']);
            return ft(200,'赠送记录已生成',$record);
        }else{
            return ft(402,'该道具数量不足以赠送');
        }
    }


    /**
     * @return array
     * [删除赠送记录]
     */
    public function delRecord()
    {
        $id = input('get.id');
        $user_id = OnlineUtils::getUserIdBySid();
        $record = Db::name('my_give')->find($id);
        if ( $user_id == $record['user_id'] ){
            Db::query('update ft_my_prop set num = num + 1 where user_id='.$user_id.' and prop_id='.$record['prop_id']);
            Db::name('my_give')->where('id','eq',$id)->delete();
            return ft(200,'删除赠送记录成功');
        }else{
            return ft(402,'删除赠送记录失败');
        }

    }



}
